#include <gflags/gflags.h>

#include <memory>

#include "data_es.h"
#include "elastic.h"
#include "log.h"

DEFINE_bool(run_mode, false, "程序的运行模式, false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(es_host, "http://127.0.0.1:9200/", "es服务器URL");

int main(int argc, char* argv[]) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    chat::initLogger(FLAGS_log_level, FLAGS_run_mode, FLAGS_log_file);

    std::shared_ptr<elasticlient::Client> es_client =
        chat::ESClientFactory::create({FLAGS_es_host});

    std::shared_ptr<chat::ESMessage> es_message =
        std::make_shared<chat::ESMessage>(es_client);

    // es_message->createIndex();
    // es_message->appendData(
    //     "会话ID-1", "消息ID-1",
    //     boost::posix_time::time_from_string("2025-8-1 19:33:22.000"),
    //     "这是一条消息-1", "用户ID-1");
    // es_message->appendData(
    //     "会话ID-1", "消息ID-2",
    //     boost::posix_time::time_from_string("2025-8-1 19:34:22.000"),
    //     "这是一条消息-2", "用户ID-2");
    // es_message->appendData(
    //     "会话ID-2", "消息ID-3",
    //     boost::posix_time::time_from_string("2025-8-1 19:35:22.000"),
    //     "这是一条消息-3", "用户ID-1");

    auto res = es_message->search("这是一条消息", "会话ID-1");
    for (auto& u : res) {
        std::cout << "-----------------" << std::endl;
        std::cout << u.chat_session_id() << std::endl;
        std::cout << u.message_id() << std::endl;
        std::cout << *u.message_content() << std::endl;
        std::cout << u.user_id() << std::endl;
    }

    // es_message->remove("消息ID-1");

    // res = es_message->search("这是一条消息", "会话ID-1");
    // for (auto& u : res) {
    //     std::cout << "-----------------" << std::endl;
    //     std::cout << u.chat_session_id() << std::endl;
    //     std::cout << u.message_id() << std::endl;
    //     std::cout << u.message_content() << std::endl;
    //     std::cout << u.user_id() << std::endl;
    // }

    return 0;
}